package com.csg.ses.service.ses.impl;

import com.csg.ses.common.utils.CommonUtil;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import lombok.RequiredArgsConstructor;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import org.springframework.transaction.annotation.Transactional;
import com.csg.ses.vo.login.UserContext;
import com.csg.ses.vo.login.UserView;
import com.csg.ses.vo.common.IdListVo;
import java.time.LocalDateTime;

import com.csg.ses.dao.ses.GroupTypeDao;
import com.csg.ses.entity.ses.GroupTypeEntity;
import com.csg.ses.service.ses.GroupTypeService;

/**
 * 
 * @author cjj
 * @date 2021-04-11 11:20:47
 */
@Service
@RequiredArgsConstructor
public class GroupTypeServiceImpl extends ServiceImpl<GroupTypeDao, GroupTypeEntity> implements GroupTypeService {
    private final GroupTypeDao groupTypeDao;

    @Override
    public Result retrieve(GroupTypeEntity groupType, Long current, Long size) {
        try {
            Page<GroupTypeEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<GroupTypeEntity> qw = new QueryWrapper();
            return new Result(groupTypeDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional
    public Result del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                int num = groupTypeDao.deleteBatchIds(idList.getIdList());
                return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(groupTypeDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public boolean save(GroupTypeEntity groupType) {
        try {
            if (!CommonUtil.isEmpty(groupType.getId())) {
                groupTypeDao.updateById(groupType);
            } else {
                groupType.setId(groupTypeDao.getNextId().toString());
                groupTypeDao.insert(groupType);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result queryAllGroupType() {
        return new Result(groupTypeDao.getAllGroupType());
    }
}